package org.cid54.bukkit.command;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.event.player.PlayerChatEvent;
import org.cid54.bukkit.Cid54PlayerListener;
import org.cid54.bukkit.command.util.PlayerCommand;
import org.cid54.bukkit.model.BluePrint;
import org.cid54.bukkit.model.BluePrintBlock;
import org.cid54.bukkit.model.BluePrintDao;

/**
 *
 * @author fgo
 */
public class BluePrintCommand implements PlayerCommand {
    
    @Override
    public boolean execute(String command, PlayerChatEvent event, Cid54PlayerListener listener) {
        
        String[] commands = command.split(" ");
        
        if (commands.length > 1) {
            
            String fileName = commands[1];
            
            BluePrint b = BluePrintDao.instance.getBluePrint(fileName);

            if(b == null){
               return false;
            }

            //Draw the bluePrint            
            final Location loc = Cid54PlayerListener.cloneLocation(event.getPlayer().getLocation());
            final World w = event.getPlayer().getWorld();
            
            for (BluePrintBlock block : b.getBlocks()) {
                
                Location l = Cid54PlayerListener.cloneLocation(loc);
                
                l.setX(l.getX() + block.getX());
                l.setY(l.getY() + block.getY());
                l.setZ(l.getZ() + block.getZ());
                
                w.getBlockAt(l).setType(Material.getMaterial(block.getMatId()));
                
            }
            
            return true;
        } else {
            return false;
        }
        
    }
    
    @Override
    public String getCommand() {
        return "blueprint";
    }
    
    @Override
    public String getHelpText() {
        return "blueprint <fileName>";
    }
}
